<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>编辑权限</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="${ctx}/rs/js/jquery.validate.min.js"></script>
<script src="${ctx}/rs/js/jquery.form.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" class="form-horizontal" action="${ctx}/permission/save.htm" method="post" enctype="multipart/form-data">
      <div class="form-group">
        <label for="name" class="col-sm-2 control-label item wid1">权限名称：</label>
        <div class="col-sm-6 wid2">
			 <input type="hidden" id="id" name="id" value="${po.id}"/>
			 <input type="text" class="form-control " id="permName" name="permName" value="${po.permName}"/>
        </div>
        <span class="help-block col-sm-4" id="valierr" style="color:#FF9966">*</span>
      </div>
      
      <div class="form-group">
        <label for="name" class="col-sm-2 control-label item wid1">授权名称：</label>
        <div class="col-sm-6 wid2">
          <input type="text" class="form-control " id="authorizName" name="authorizName" value="${po.authorizName}"/>
        </div>
        <span class="help-block col-sm-4" id="valierr" style="color:#FF9966">*</span>
      </div>
      
      <div class="form-group">
        <label for="name" class="col-sm-2 control-label item wid1">访问路径：</label>
        <div class="col-sm-6 wid2">
          <input type="text" class="form-control " id="url" name="url" value="${po.url}"/>
        </div>
        <span class="help-block col-sm-4" id="valierr" style="color:#FF9966">*</span>
      </div>
      
	  <div class="form-group">
        <label for="name" class="col-sm-2 control-label item wid1">上级编号：</label>
        <div class="col-sm-6 treepost wid2">
            <input type="hidden" id="permLevel" name="permLevel" value="${po.permLevel}"/>
            <input type="hidden" id="parentId" name="parentId" value="${po.parentId}"/>
			<input type="text" class="form-control  treeicon" readonly="readonly" id="treeSelect" name="treeSelect" onclick="showZTree();"/>
			<div id="comboBoxCase" class="menuContent" style="display:none; position: absolute; z-index:9999">
	           <ul id="comboBoxTree" class="ztree" style="height:160px;width:283px;overflow:auto;z-index:9999"></ul>
            </div>
        </div>
      </div>
      
      <div class="form-group">
        <label for="name" class="col-sm-2 control-label item wid1">图标路径：</label>
        <div class="col-sm-6 wid2">
          <input type="text" class="form-control "  id="iconUrl" name="iconUrl" value="${po.iconUrl}"/>
        </div>
        <span class="help-block col-sm-4" id="valierr" style="color:#FF9966">*</span>
      </div>
      
	  <div class="form-group">
        <label for="name" class="col-sm-2 control-label item wid1">描述：</label>
        <div class="col-sm-6 wid2">
		  <textarea class="form-control" rows="3" id="describes" name="describes">${po.describes}</textarea>
        </div>
      </div>
	  
      <div class="form-group">
        <div class="col-sm-10 text-center pt15">
		  <shiro:hasPermission name="permission:update">
		     <button type="button" class="btn btn-primary btn-lg srt-btn active mr30" onclick="valiSubmit()">提交</button>
		     <button type="button" class="btn btn-default btn-lg srt-btn active" onclick="onReset()">重置</button>
          </shiro:hasPermission>
        </div>
      </div>

      <div class="form-group">
        <div class=" col-sm-10">
          <span id="error-text" style="color: #FF0000;"></span>
        </div>
      </div>
</form>


<script type="text/javascript" src="${ctx}/rs/js/bootstrapQ.min.js"></script>
<script src="${ctx}/rs/js/messages_zh.min.js"></script>
<script type="text/javascript">
	
	
	//初始化验证 start
	var form = $('#form1');
	form.validate({
		rules : {
			permName : {
				maxlength : 100,
				required : true
			},
			authorizName : {
				maxlength : 255,
				required : true
			},
			url : {
				maxlength : 200,
				required : true
			},
			iconUrl : {
				maxlength : 200,
				required : true
			}
		},
		highlight : function(element) {
			$(element).closest('.form-group').removeClass('success').addClass(
					'error');
		},
		success : function(element) {
			element.text('OK!').addClass('valid').closest('.form-group')
					.removeClass('error').addClass('success');
		}
	});
	/** end */

	//ztree初始化
	var zSetting = {
		view : {
			dblClickExpand : false
		},
		data : {
			key : {
				// 				children: "permissions",
				name : "permName",
				url : "#"
			},
			simpleData : {
				enable : true,
				idKey : "id",
				pIdKey : "parentId"
			}
		},
		callback : {
			onClick : onZClick
		}
	};


	$.ajax({
				url : "${ctx}/permission/tree?filterId=${po.id}",
				success : function(data) {
					var zNodes = eval(data);
					var treeObj = $.fn.zTree.init($("#comboBoxTree"), zSetting,
							zNodes);
					var pid = $("#parentId").val();
					var node = treeObj.getNodeByParam("id", pid, null);
					if (node) {
						treeObj.checkNode(node, true, false);
						$("#treeSelect").val(node.permName);
					}
				},
				error : function() {
					bootstrapQ.msg({
						msg : '树状下拉列表加载失败！',
						type : 'danger',
						time : 2000
					});
				}
			});

	function onZClick(e, treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj("comboBoxTree"), nodes = zTree
				.getSelectedNodes(), d = "", v = "", p = 0;
		for (var i = 0, l = nodes.length; i < l; i++) {
			var node = nodes[i];
			d = node.id;
			v = node.permName;
			p = node.permLevel;
		}
		$("#parentId").attr("value", d);
		$("#treeSelect").attr("value", v);
		$("#permLevel").val(p + 1);//等级+1
		hideZTree();
	}

	function showZTree() {
		var cityObj = $("#treeSelect");
		var cityOffset = $("#treeSelect").offset();
		$("#comboBoxCase").css({
			left : "15px",
			top : "37px"
		}).slideDown("fast");

		$("body").bind("mousedown", onZBodyDown);
	}

	function hideZTree() {
		$("#comboBoxCase").fadeOut("fast");
		$("body").unbind("mousedown", onZBodyDown);
	}

	function onZBodyDown(event) {
		if (!(event.target.id == "comboBoxCase" || $(event.target).parents(
				"#comboBoxCase").length > 0)) {
			hideZTree();
		}
	}

	function valiSubmit() {
		if (form.valid()) {
			var data = $("#form1").serialize(); //获取表单多个控件的数据
			$(".unable").show();
			$.ajax({
				url : "${ctx}/permission/save.htm",
				type : "post",
				data : data,
				success : function(text) {
					if (!$('.unable').is(':hidden')) {
						$(".unable").hide();
					}
					// 普通消息提示条
					bootstrapQ.msg({
						msg : '操作成功！',
						type : 'success',
						time : 2000
					});
					//触发隐藏确认的按钮，引用回调方法关闭窗口并刷新列表
					$("button.bsok").click();
				}
			});
		}
	}

	function onReset() {
		$("#permName").val("");
		$("#authorizName").val("");
		$("#url").val("");
		$("#iconUrl").val("");
		$("#describes").val("");
	}
</script> 
</body>
</html>